﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MonsterGenerateScript : MonoBehaviour
{
    public GameObject objectPrefab;                   //预制件
    public float timeInterval = 5;                    //生成时间间隔 单位s
    private float rotationY = 0;                       //生成物体的初始旋转角度
    public bool NumLimit = false;                     //是否需要控制数量
    public int MaxNum = 100;                          //限制数量状态下的最大数量 
    public bool asChildComponent = false;             //是否以子组件形式生成
    private float timeBuffer = 0;                     //缓冲时间计量 当生成的目标含有对应摧毁时调用时会减少
    private int currentNum = 0;                       //当前数量

    void FixedUpdate()
    {
        if (checkNum())
        {
            timeBuffer += Time.deltaTime;
            if (timeInterval <= timeBuffer)
            {
                //初始化计数器
                timeBuffer = 0;
                //从预制体初始化对象
                GameObject temp = Instantiate(objectPrefab);
                //旋转物体
                temp.transform.Rotate(0, rotationY, 0);
                //设置物体属性
                if (asChildComponent)
                {
                    temp.transform.parent = this.transform; //设置父物体
                }
                //设置放置位置
                temp.transform.position = this.transform.position;
            }
        }
    }
    //检测Enemy数量
    bool checkNum()
    {
        if (NumLimit)
        {
            currentNum = GameObject.FindGameObjectsWithTag("Enemy").Length;
            if (currentNum >= MaxNum) return false;
        }
        return true;
    }
}
